package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.controller.dto.DossierDto;
import com.yqg.manage.entity.Archive;
import com.yqg.manage.entity.Case;
import com.yqg.manage.mapper.DossierMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IDossierService;
import com.yqg.manage.entity.Dossier;

import org.springframework.web.bind.annotation.RestController;

/**
 * DossierController 前端控制器
 *
 * @author zxf
 * @since 2024-06-06
 */
@RestController
@RequestMapping("/dossier")
public class DossierController {

    @Resource
    private IDossierService dossierService;

    @Autowired
    private DossierMapper dossierMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新卷宗")
    public Result save(@RequestBody Dossier dossier) {
        LambdaQueryWrapper<Dossier> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dossier::getDossierNo, dossier.getDossierNo());
        if (dossier.getId() != null) {
            queryWrapper.ne(Dossier::getId, dossier.getId());
        }
        Dossier one = dossierService.getOne(queryWrapper);
        if (one != null) {
            return Result.error("该案件编号已存在");
        }
        if (dossier.getId() == null) {
            dossier.setCreateTime(new Date());
        }
        return Result.success(dossierService.saveOrUpdate(dossier));
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    @SysLog("删除卷宗")
    public Result delete(@PathVariable Integer id) {
        Dossier dossier = dossierService.getById(id);
        try {
            dossier.setIsDelete("1");
        } catch (Exception e) {
            return Result.error("已删除");
        }
        return Result.success(dossierService.saveOrUpdate(dossier));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Dossier> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Dossier> list = dossierService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(dossierService.getById(id));
    }

    /**
     * 分页查询
     *
     * @param userId 副主任查其他部门
     * @param unitId 查询单独部门
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String search,
                           @RequestParam(required = false) Long userId,
                           @RequestParam(required = false) Long unitId) {
        pageNum = pageNum * pageSize - pageSize;
        List<Dossier> dossierList = dossierMapper.getPage(pageNum, pageSize, search, userId, unitId);
        Integer total = dossierMapper.getPageCount(search, userId, unitId);
        Map<String, Object> map = new HashMap<>();
        map.put("records", dossierList);
        map.put("total", total);
        return Result.success(map);
    }

    /**
     * 根据条件获取所有卷宗名称
     */
    @GetMapping("/getAllDossierName")
    public Result getAllDossierName(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(required = false) String search,
                                    @RequestParam(required = false) Long userId,
                                    @RequestParam(required = false) Long unitId) {
        pageNum = (pageNum - 1) * pageSize;
        List<DossierDto> dossierDtos = dossierMapper.getAllDossierName(pageNum, pageSize, search, userId, unitId);
        Integer total = dossierMapper.getAllDossierNameCount(search, userId, unitId);
        Map<String, Object> map = new HashMap<>();
        map.put("data", dossierDtos);
        map.put("total", total);
        return Result.success(map);
    }
}

